package com.szx.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szx.ssm.bean.Msg;
import com.szx.ssm.bean.tbEmp;
import com.szx.ssm.servlet.tbEmpServlet;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.FileFilter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author songzx
 * @create 2022-06-28 15:16
 */
@Controller
public class IndexController {
    @Autowired
    tbEmpServlet tbEmpServlet;

    /*@GetMapping("/")
    public String index(@RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,Model model){
        PageInfo pageInfo = tbEmpServlet.getEmpList(pageNumber);
        model.addAttribute("pageInfo",pageInfo);
        return "index";
    }*/

    @GetMapping("/")
    public String index(){
        return "index";
    }

    // 返回json格式的数据
    @GetMapping("/getEmpList/{pageNum}")
    @ResponseBody
    public Msg getEmpList(@PathVariable("pageNum") Integer pageNum){
        PageInfo empList = tbEmpServlet.getEmpList(pageNum);
        Msg msg = Msg.success().add("empList", empList);
        return msg;
    }

    /**
     * 模糊查询
     * @author Songzx
     * @date 2022/7/3
     */
    @GetMapping("/getEmpList/{pageNum}/{empName}")
    @ResponseBody
    public Msg getEmpListByName(@PathVariable("pageNum") Integer pageNum,
                                @PathVariable("empName") String name ){
        PageInfo empListByName = tbEmpServlet.getEmpListByName(pageNum, name);
        Msg msg = Msg.success().add("empList", empListByName);
        return msg;
    }

    /**
     * 根据id删除
     * @author Songzx
     * @date 2022/7/3
     */
    @DeleteMapping("/deleteEmp/{id}")
    @ResponseBody
    public Msg deleteEmpByid(@PathVariable("id") Integer id){
        tbEmpServlet.deleteEmpById(id);
        Msg msg = Msg.success();
        return msg;
    }

    /**
     * 增加或者删除
     * @author Songzx
     * @date 2022/7/3
     */
    @PostMapping("/andOrUpdateEmp")
    @ResponseBody
    public Msg addOrUpdateEmp(@Valid @RequestBody tbEmp emp, BindingResult result){
        // 判断是否有校验出错的情况
        if(result.hasErrors()){
            // 获取发生错误的字段数组
            List<FieldError> fieldErrors = result.getFieldErrors();
            // 添加一个存放错误的map
            HashMap<String, String> errorMap = new HashMap<>();
            // 遍历所有的错误字段
            for (FieldError fieldError : fieldErrors) {
                // 获取那个字段错误了
                String key = fieldError.getField();
                // 错误信息
                String message = fieldError.getDefaultMessage();
                // put到map中
                errorMap.put(key,message);
            }
            // 返回信息给前端
            return Msg.error().add("errInfo",errorMap);
        }else{
            if(emp.getEmpId() == null){
                tbEmpServlet.addEmpInfo(emp);
            }else {
                tbEmpServlet.updateEmp(emp);
            }
            return Msg.success();
        }
    }

    /**
     * 批量删除
     * @author Songzx
     * @date 2022/7/3
     */
    @PostMapping("/deleteEmpByIds")
    @ResponseBody
    public Msg deleteEmpBuIds(@RequestBody Map<String,List<Integer>> map){
        List<Integer> ids = map.get("ids");
        tbEmpServlet.deletesEmp(ids);
        return Msg.success();
    }

}
